﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class newsreading : System.Web.UI.Page
    {
        private int id = 0;
        private int cid = 0;

        protected void Page_Load(object sender, EventArgs e)
        { 
            if (!IsPostBack)
            {
                UpdateParamInfoLogic.UpdateAllNewsAbout();
                try 
                {
                    id = Int32.Parse(Request.QueryString["id"].ToString().Trim());
                }
                catch
                {
                    id = 0;
                    cid = Int32.Parse(Request.QueryString["cid"].ToString().Trim());
                }
                try
                {
                    cid = Int32.Parse(Session["NCategoryID"].ToString());
                }
                catch
                {
                    cid = Int32.Parse(Request.QueryString["cid"].ToString().Trim());
                }
                ArrayList prelist = NewsInfoLogic.GetPreNewsInfo(id,cid);
                ArrayList nextlist = NewsInfoLogic.GetNextNewsInfo(id,cid);

                if ((Int32.Parse(prelist[2].ToString().Trim()) > 0) && (Int32.Parse(nextlist[2].ToString().Trim()) > 0))
                {
                    btn_pre.ImageUrl = "~/images/wnp.png";
                    btn_next.ImageUrl = "~/images/wnn.png";
                    btn_pre.Enabled = true;
                    btn_next.Enabled = true;
                }
                else if ((Int32.Parse(prelist[2].ToString().Trim()) > 0) && (Int32.Parse(nextlist[2].ToString().Trim()) <= 0))
                {
                    btn_pre.ImageUrl = "~/images/wnp.png";
                    btn_next.ImageUrl = "~/images/bnn.png";
                    btn_pre.Enabled = true;
                    btn_next.Enabled = false;
                }
                else if ((Int32.Parse(prelist[2].ToString().Trim()) <= 0) && (Int32.Parse(nextlist[2].ToString().Trim()) > 0))
                {
                    btn_pre.ImageUrl = "~/images/bnp.png";
                    btn_next.ImageUrl = "~/images/wnn.png";
                    btn_pre.Enabled = false;
                    btn_next.Enabled = true;
                }
                else
                {
                    btn_pre.ImageUrl = "~/images/bnp.png";
                    btn_next.ImageUrl = "~/images/bnn.png";
                    btn_pre.Enabled = false;
                    btn_next.Enabled = false;
                }

                NewsCategoryBindData(cid);
                NewInfoBindData(id);

                string countScript = " $($.post('counter.aspx', {cid:'" + id + "'},function(data){}));";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "NewsCount", countScript, true);
            }         
        }

        public void NewInfoBindData(int id)
        {
            List<NewsInfo> NewsInfoList = NewsInfoLogic.GetNewsInfo(id);
            if (NewsInfoList == null)
            {
                NewsInfoList = new List<NewsInfo>();
                NewsInfoList.Add(new NewsInfo());
            }

            NewsInfoData.DataSource = NewsInfoList;
            NewsInfoData.DataBind();

            /*绑定新闻标题*/
            NewsInfo news = NewsInfoList.FirstOrDefault();
            Page.Title = news.Title;
        }

        public void NewsCategoryBindData(int CategoryID)
        {
            List<NewsCategoryInfo> newsCategoryList = NewsCategoryInfoLogic.GetAllNewsCategoryInfo(CategoryID);

            if (newsCategoryList == null)
            {
                newsCategoryList = new List<NewsCategoryInfo>();
                newsCategoryList.Add(new NewsCategoryInfo());
            }

            NewsCategoryRepeater.DataSource = newsCategoryList;
            NewsCategoryRepeater.DataBind();
        }

        protected void btn_pre_Click(object sender, ImageClickEventArgs e)
        {
            id = Int32.Parse(Request.QueryString["id"].ToString().Trim());
            try
            {
                cid = Int32.Parse(Session["NCategoryID"].ToString());
            }
            catch
            {
                cid = Int32.Parse(Request.QueryString["cid"].ToString().Trim());
            }
            try
            {
                cid = Int32.Parse(Session["NCategoryID"].ToString());
            }
            catch
            {
                cid = Int32.Parse(Request.QueryString["cid"].ToString().Trim());
            }
            ArrayList prelist = NewsInfoLogic.GetPreNewsInfo(id,cid);
            int preNewsID = Int32.Parse(prelist[0].ToString().Trim());
            int preNewsCategoryID = Int32.Parse(prelist[1].ToString().Trim());
            if (preNewsID != 0)
            {
                Response.Redirect("newsreading.aspx?cid="+preNewsCategoryID+"&"+"id="+preNewsID);
            }
        }

        protected void btn_next_Click(object sender, ImageClickEventArgs e)
        {
            id = Int32.Parse(Request.QueryString["id"].ToString().Trim());
            try
            {
                cid = Int32.Parse(Session["NCategoryID"].ToString());
            }
            catch
            {
                cid = Int32.Parse(Request.QueryString["cid"].ToString().Trim());
            }
            ArrayList nextlist = NewsInfoLogic.GetNextNewsInfo(id,cid);
            int nextNewsID = Int32.Parse(nextlist[0].ToString().Trim());
            int nextNewsCategoryID = Int32.Parse(nextlist[1].ToString().Trim());
            if (nextNewsID != 0)
            {
                Response.Redirect("newsreading.aspx?cid=" + nextNewsCategoryID + "&" + "id=" + nextNewsID);
            }
        }
    }
}
